package com.five.wisdomClass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.wisdomClass.entity.*;
import com.five.wisdomClass.mapper.StudentMapper;
import com.five.wisdomClass.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements IStudentService {

    @Autowired
    StudentMapper studentMapper;

    // 获取个人所在班级的同班同学
    @Override
    public IPage<Student> getSpecializedClassMember(Integer currentPage, Integer size,String sNumber) {
        Student student = studentMapper.getStudent(sNumber);
        // 通过专业班级查询学生的姓名，学号，电话 获取专业班级名称
        String classname = student.getClassname();

        Page<Student> pagerequest = new Page<>(currentPage,size);
        IPage<Student>  studentIPage = studentMapper.getClassMates(pagerequest,classname);
        return studentIPage;
    }

    // 获取个人信息
    @Override
    public Student getSelfInfo(String sNumber) {
        // 查看个人信息 学号 姓名 院系 专业 班级
        return studentMapper.getSelfInfo(sNumber);
    }

    // 获取公共课程信息
    @Override
    public List<Course> getPublicCourses(String sNumber) {
        Student student = studentMapper.getStudent(sNumber);
        if (student == null){
            return null;
        }
        // 获取班级名
        String classname = student.getClassname();
        if (classname == null){
            return null;
        }
        // 获取班级号
        String classId = studentMapper.getClassId(classname);
        if (classId == null){
            return null;
        }
        // 获取课程号
        List<String> courseNumberList = studentMapper.getPublicCourseNumber(classId);
        if (courseNumberList == null ||courseNumberList.isEmpty()){
            return null;
        }
        // 获取课程信息
        List<Course> publicCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            publicCourseList.addAll(studentMapper.getPublicCourse(courseNumber));
        }
        return publicCourseList;
    }

    // 获取选修课程信息
    @Override
    public List<Course> getTakeCourses(String sNumber) {
        Student student = studentMapper.getStudent(sNumber);
        if (student == null){
            return null;
        }

        // 查询课程号
        List<String> courseNumberList = studentMapper.getTakeCourseNumbers(sNumber);
        if (courseNumberList == null || courseNumberList.isEmpty()){
            return null;
        }
        // 查询课程信息
        List<Course> takeCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            takeCourseList.addAll(studentMapper.getTakeCourse(courseNumber));
        }
        return takeCourseList;
    }

    // 获取个人课程分数
    @Override
    public List<Scores> getSelfScores(String sNumber) {

        // 通过学号查询课程成绩
        List<Scores> scoresList = studentMapper.getScores(sNumber);
        return scoresList;
    }

    // 添加个人计划
    @Override
    public boolean addPlan(SelfPlan selfPlan) {
        if (selfPlan.getSNumber() == null){
            return false;
        }
        selfPlan.setDate(new Date());
        return studentMapper.addPlan(selfPlan) > 0;
    }

    // 删除个人计划
    @Override
    public boolean deletePlan(Integer id) {
        return studentMapper.deletePlan(id) > 0;
    }

    // 修改个人计划
    @Override
    public boolean editPlan(String context, Integer id) {
        return studentMapper.editPlan(context,id) > 0;
    }

    // 查看个人计划
    @Override
    public IPage<SelfPlan> getPlans(Integer currentPage, Integer size, String sNumber) {
        if (sNumber == null){
            return null;
        }
        Page<SelfPlan> pagerequest = new Page<>(currentPage,size);
        IPage<SelfPlan>  selfPlanIPage = studentMapper.showPlan(pagerequest,sNumber);
        return selfPlanIPage;
    }

    // 学生请假
    @Override
    public boolean submitLeave(Leave leave) {
        // 条件判断
        if (leave.getLeaveType() == null){
            return false;
        }
        if (leave.getStartTime() == null || leave.getEndTime() == null){
            return false;
        }
        if (leave.getContact() == null || leave.getContext() == null){
            return false;
        }

        return studentMapper.submitLeave(leave) > 0;
    }

    // 查看请假列表
    @Override
    public IPage<Leave> getSelfLeaves(Integer currentPage, Integer size, String sNumber) {
        if (sNumber == null){
            return null;
        }
        Page<Leave> pagerequest = new Page<>(currentPage,size);
        IPage<Leave>  leavesIpage = studentMapper.getSelfLeaves(pagerequest,sNumber);
        return leavesIpage;
    }

    // 获取公共课
    @Override
    public List<Course> getSelfPublicCourses(String sNumber) {
        Student student = studentMapper.getStudent(sNumber);
        if (student == null){
            return null;
        }
        // 获取班级名
        String classname = student.getClassname();
        if (classname == null){
            return null;
        }
        // 获取班级号
        String classId = studentMapper.getClassId(classname);
        if (classId == null){
            return null;
        }
        // 获取课程号
        List<String> courseNumberList = studentMapper.getSelfPublicCourseNumber(classId);
        if (courseNumberList == null ||courseNumberList.isEmpty()){
            return null;
        }
        // 获取课程信息
        List<Course> publicCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            publicCourseList.addAll(studentMapper.getSelfPublicCourse(courseNumber));
        }
        return publicCourseList;
    }

    // 获取选修课
    @Override
    public List<Course> getSelfTakeCourses(String sNumber) {

        // 查询课程号
        List<String> courseNumberList = studentMapper.getSelfTakeCourseNumbers(sNumber);
        if (courseNumberList == null || courseNumberList.isEmpty()){
            return null;
        }
        // 查询课程信息
        List<Course> takeCourseList = new ArrayList<>();
        for (String courseNumber : courseNumberList) {
            takeCourseList.addAll(studentMapper.getSelfTakeCourse(courseNumber));
        }
        return takeCourseList;
    }

    // 获取某课程的签到信息
    @Override
    public IPage<SignInfo> getSignInfo(Integer currentPage, Integer size, String courseNumber, String sNumber) {
        Page<SignInfo> pagerequest = new Page<>(currentPage,size);
        IPage<SignInfo>  signInfoIpage = studentMapper.getSignInfos(pagerequest,courseNumber,sNumber);
        return signInfoIpage;
    }

    // 签到
    @Override
    public boolean sign(SignInfo signInfo) {
        return studentMapper.studentSign(signInfo) > 0;
    }

    // 签到超时
    @Override
    public boolean unSign(SignInfo signInfo) {
        return studentMapper.studentUnSign(signInfo) > 0;
    }
}
